package com.ys.malini.web.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.ys.malini.dao.ICustomerServiceDao;
import com.ys.malini.model.TServiceinfo;
import com.ys.malini.web.form.CustomerServiceForm;

/*
 * 公司信息管理
 * 
 */
public class CustomerServiceAction extends DispatchAction {

	private ICustomerServiceDao customerDao;


	/**
	 * 跳到查询页面
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward toQuery(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		List<TServiceinfo> customerlist = customerDao.query(null,true);
		request.setAttribute("customerlist", customerlist);
		return mapping.findForward("listPage");
	}


	/**
	 * 客户服务信息查询
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward query(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		CustomerServiceForm formbean = (CustomerServiceForm)form;
		List<TServiceinfo> customerlist = customerDao.query(formbean,true);
		request.setAttribute("customerlist", customerlist);
		return mapping.findForward("listPage");
	}
	
	
	/**
	 * 客户服务信息查询前台
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward queryQt(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String serviceType = request.getParameter("serviceType");
		TServiceinfo customerinfo = customerDao.queryTop(serviceType);
		request.setAttribute("customerinfo", customerinfo);
		if("0".equals(serviceType)){
			return mapping.findForward("safe");
		}else{
			return mapping.findForward("service");
		}
		
	}
	

	/**
	 * 跳到更新页面
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward toUpdate(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String cusid = request.getParameter("serviceId");
		TServiceinfo customerinfo = customerDao.queryCustomserServiceById(cusid);
		
		request.setAttribute("customerinfo", customerinfo);
		return mapping.findForward("update");
	}

	/**
	 * 更新页面
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		CustomerServiceForm formbean =(CustomerServiceForm)form;
		System.out.println("rsv1=="+formbean.getRsv1());
		String message = customerDao.updateCustomserServiceById(formbean);
		request.setAttribute("message",message);
		return mapping.findForward("update");
	}

	/**
	 * 跳到添加页面
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward toAdd(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		return mapping.findForward("add");
	}

	/**
	 * 客户服务信息新增
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		CustomerServiceForm formbean = (CustomerServiceForm) form;
		String message = customerDao.addCustomserService(formbean);
		request.setAttribute("message", message);
		return mapping.findForward("add");
	}
	
	
	/**
	 * 公司信息删除
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String serviceId=request.getParameter("serviceId");
		String serviceType=request.getParameter("serviceType");
		Map map = new HashMap();
		map.put("serviceId", serviceId);
		map.put("serviceType", serviceType);
		String message = customerDao.deleteCustomserService(map);
		request.setAttribute("message", message);
		List<TServiceinfo> customerlist = customerDao.query(null,true);
		request.setAttribute("customerlist", customerlist);
		return mapping.findForward("listPage");
	}


	public ICustomerServiceDao getCustomerDao() {
		return customerDao;
	}


	public void setCustomerDao(ICustomerServiceDao customerDao) {
		this.customerDao = customerDao;
	}

	


	
}
